package com.ysxsoft.wdg.ui.activity.shopcart;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.bean.BaseBean;
import com.ysxsoft.wdg.bean.cart.ShopcartBean;
import com.ysxsoft.wdg.net.ApiHelper;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.util.ViewUtils;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;


public class ShopcartAdapter extends BaseQuickAdapter<ShopcartBean, BaseViewHolder> {


    private OnUpdateListener mOnUpdateListener;
    private OnItemSelectListener mOnItemSelectListener;
    private boolean isDeleteMode;
    private ApiHelper mApiHelper;

    public void setDeleteMode(boolean deleteMode) {
        isDeleteMode = deleteMode;
    }

    public void clear(){
        getData().clear();
        notifyDataSetChanged();
    }


    public ShopcartAdapter(int layoutResId) {
        super(layoutResId);
        mApiHelper = new ApiHelper();
    }

    @Override
    protected void convert(BaseViewHolder helper, final ShopcartBean item) {
        final int position = helper.getLayoutPosition();

        final TextView mEditNum = helper.getView(R.id.edit_goods_num);
        TextView tvSize = helper.getView(R.id.tv_size);
        ImageView mEditGoodsSub = helper.getView(R.id.edit_goods_sub);
        ImageView mEditGoodsPlus = helper.getView(R.id.edit_goods_plus);
        ImageView ivPic = helper.getView(R.id.iv_pic);

        mEditNum.setText(item.getNum() + "");
        tvSize.setText(item.getRule_name());
        Glide.with(mContext).load(item.getPic()).into(ivPic);

        helper.setText(R.id.tv_money, mContext.getString(R.string.price_rmb, item.getL_price() + ""));

        TextView tvOldMoney = helper.getView(R.id.tv_old_money);
        tvOldMoney.setText(mContext.getString(R.string.price_rmb, item.getPrice() + ""));
        ViewUtils.setDeletLine(tvOldMoney);

        helper.setText(R.id.tv_name, item.getName());


        ImageView mIvSelect = helper.getView(R.id.iv_selector);
        mIvSelect.setSelected(item.isSelect());

        mIvSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemSelectListener.onItemSelect(position);
            }
        });


        //        final int limit = Integer.parseInt(item.getStore());
        mEditGoodsSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (item.getNum() > 1 && mOnUpdateListener != null) {
                    updataNum(item.getNum() - 1, position, item);
                }
            }
        });


        mEditGoodsPlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnUpdateListener != null /* && item.getNum() <limit */) {
                    updataNum(item.getNum() + 1, position, item);
                }
            }
        });
        tvSize.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnUpdateListener != null) {
                    mOnUpdateListener.onSkuClick(position);
                }
            }
        });

    }


    private void updataNum(final int numP, final int position, final ShopcartBean item) {
        mApiHelper.editCartNum(item.getUid(), item.getCarid(), numP+"", new Observer<BaseBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseBean baseBean) {
                if (baseBean.getCode().equals(CallbackCode.SUCCESS)) {
                    item.setNum(numP);
                    mOnUpdateListener.onCountChange(position, item.getNum());
                    notifyDataSetChanged();
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }


    public void setOnUpdateListener(OnUpdateListener onUpdateListener) {
        mOnUpdateListener = onUpdateListener;
    }

    public interface OnUpdateListener {
        void onCountChange(int position, int num);

        void onSkuClick(int position);

        void OnPriceChange();
    }

    public void setOnItemSelectListener(OnItemSelectListener onItemSelectListener) {
        mOnItemSelectListener = onItemSelectListener;
    }

    public interface OnItemSelectListener {
        void onItemSelect(int position);
    }

}
